package com.nari.ysphd.ui;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

import com.jeremyfeinstein.okhttputil.callback.StringCallback;
import com.jeremyfeinstein.utils.ToastUtil;
import com.jeremyfeinstein.utils.YCHSInstance;
import com.jeremyfeinstein.utils.YCHShttpAPI;
import com.nari.ysphd.MyApp;
import com.nari.ysphd.util.ActivityManager;

import nari.mip.core.Platform;
import okhttp3.Call;

public abstract class BaseActivity extends FragmentActivity {

    private static final String TAG = BaseActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //点击图标不重启app
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }

        setContentView();
        initViews();
        initListeners();
        initData();
    }


    protected void initData() {
    }

    protected void initListeners() {
    }

    protected void initViews() {
    }

    protected void setContentView() {
    }

    public void showWithStatus(final String status) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ToastUtil.showLong(MyApp.getApp(), status);
            }
        });
    }

    public void showInfoWithStatus(final String infoWithStatus) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ToastUtil.showLong(MyApp.getApp(), infoWithStatus);
            }
        });
    }

    public void showSuccessWithStatus(final String successWithStatus) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ToastUtil.showLong(MyApp.getApp(), successWithStatus);
            }
        });
    }

    public void showErrorWithStatus(final String errorWithStatus) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ToastUtil.showLong(MyApp.getApp(), errorWithStatus);
            }
        });
    }

    public void promptDialog(String msg, final PromptListener promptListener) {
        AlertDialog.Builder builder = new Builder(this);
        builder.setMessage(msg);
        builder.setTitle("提示");
        builder.setPositiveButton("确定", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                promptListener.callBack();
            }
        });
        builder.create().show();
    }

    public void prompt2Dialog(String msg, final PromptListener promptListener) {
        AlertDialog.Builder builder = new Builder(this);
        builder.setMessage(msg);
        builder.setTitle("提示");
        builder.setPositiveButton("是", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                promptListener.callBack();
            }
        });
        builder.setNegativeButton("否", null);
        builder.create().show();
    }

    public interface PromptListener {
        void callBack();
    }

    protected void logout() {

        YCHSInstance.resetInstance();
        YCHShttpAPI.LogOut(MyApp.getApp(), new StringCallback() {
            @Override
            public void onResponse(String response) {
                jumpToMipLogin();
            }

            @Override
            public void onError(Call call, int statusCode, Exception e) {
                Log.e(TAG, "退出登录失败，无法连接服务！");
                jumpToMipLogin();
            }
        });
    }


    /**
     * 跳到MIP的登录界面。
     */
    public void jumpToMipLogin() {
        MyApp.isLoginOut = true;
        ActivityManager.getInstance().finishActivityExcept(IPConfigActivity.class);
        Platform.getCurrent().stop(this);
    }

}
